package com.jackstraw.jack_base.datasource.zookeeper.config;

import com.jackstraw.jack_base.datasource.common.config.DataSourceConfiguration;
import com.jackstraw.jack_base.datasource.zookeeper.bean.ZookeeperFactory;
import com.jackstraw.jack_base.datasource.zookeeper.bean.ZookeeperProperties;
import com.jackstraw.jack_base.datasource.zookeeper.bean.ZookeeperPropertiesEntity;
import com.jackstraw.jack_base.datasource.zookeeper.bean.ZookeeperRouting;
import com.jackstraw.jack_base.datasource.zookeeper.bean.ZookeeperTemplate;
import com.jackstraw.jack_base.datasource.zookeeper.bean.ZookeeperTemplateCut;
import com.jackstraw.jack_base.datasource.zookeeper.exception.BadZookeeperException;
import com.jackstraw.jack_base.util.ObjectUtil;
import com.jackstraw.jack_base.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.Environment;

@Slf4j
@Configuration
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
public class ZookeeperConfiguration
        extends DataSourceConfiguration<ZookeeperRouting,
        ZookeeperPropertiesEntity,
        ZookeeperTemplate,
        ZookeeperFactory,
        ZookeeperTemplateCut,
        ZookeeperProperties,
        ZookeeperConfiguration> {

    public ZookeeperConfiguration(ApplicationContext applicationContext, Environment environment) {
        super(applicationContext, environment);
    }

    @Override
    @Bean("zookeeperProperties")
    @ConfigurationProperties(prefix = "jack.datasource.zookeeper")
    public ZookeeperProperties properties() {
        return new ZookeeperProperties();
    }

    @Override
    @Bean("zookeeperBuildFactory")
    public ZookeeperFactory dataSourceBuildFactory() {
        return new ZookeeperFactory();
    }

    @Override
    @Bean("zookeeperTemplateCut")
    @ConditionalOnBean(name = {"zookeeperBuildFactory"})
    public ZookeeperTemplateCut dataSourceTemplateCut(ZookeeperFactory dataSourceBuildFactory) {
        return new ZookeeperTemplateCut(dataSourceBuildFactory);
    }

    @Override
    @Bean("zookeeperRouting")
    @ConditionalOnBean(name = {"zookeeperProperties", "zookeeperBuildFactory", "zookeeperTemplateCut"})
    public ZookeeperRouting routing() {
        ZookeeperProperties properties = SpringUtil.getBean("zookeeperProperties", ZookeeperProperties.class);
        if (ObjectUtil.ObjectUtilByLang3.isEmpty(properties)) {
            log.error("zookeeper配置文件异常");
            throw new BadZookeeperException("037", "zookeeper配置文件异常");
        }

        ZookeeperTemplateCut dataSourceTemplateCut = SpringUtil.getBean("zookeeperTemplateCut", ZookeeperTemplateCut.class);
        if (ObjectUtil.ObjectUtilByLang3.isEmpty(dataSourceTemplateCut)) {
            log.error("zookeeper切面异常");
            throw new BadZookeeperException("038", "zookeeper切面异常");
        }

        ZookeeperRouting zookeeperRouting = new ZookeeperRouting();
        zookeeperRouting.routing(properties, dataSourceTemplateCut);
        return zookeeperRouting;
    }
}
